package com.kucai.yifaqapp.presenter.activity;

import android.widget.Toast;

import com.google.gson.Gson;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResLogin;
import com.kucai.yifaqapp.ui.activity.LoginActivity;
import com.kucai.yifaqapp.util.SpUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivityPresenter extends BaseActivityPresenter<LoginActivity> {
    public LoginActivityPresenter(LoginActivity mActivity) {
        super(mActivity);
    }

    public void login(String username, String password) {
        //这里开始网络请求
        HttpJsonUtils.getHttpApiInstance()
                .login(HttpAdress.getuseAdress() + "api/m/login",username, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResLogin>(mActivity) {
                    @Override
                    public void onNext(ResLogin resLogin) {
                        if (resLogin.code == 0 && resLogin.data != null) {
                            Info.user = resLogin.data;
                            SpUtils.clearUser(mActivity);
                            SpUtils.putString(mActivity, "User", new Gson().toJson(resLogin.data));
                            String user = SpUtils.getString(mActivity, "User");
                            if (new Gson().fromJson(user, ResLogin.DataBean.class) == null) {
                                String user1 = SpUtils.getString(mActivity, "User");
                                if (new Gson().fromJson(user1, ResLogin.DataBean.class) == null) {
                                }
                                Info.user = new Gson().fromJson(user1, ResLogin.DataBean.class);
                            }
                            mActivity.onsuccess();
                        } else {
                            mActivity.onfail(resLogin);
                        }
                    }

                    @Override
                    protected void onCustomNext(ResLogin resLogin) {

                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }


    /**
     * 试玩登录
     */
    public void logindemo() {
        HttpJsonUtils.getHttpApiInstance()
                .demologin(HttpAdress.getuseAdress() + "api/m/guestLogin")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResLogin>(mActivity) {

                    @Override
                    protected void onCustomNext(ResLogin resLogin) {
                        if (resLogin.code == 0) {
                            Info.user = resLogin.data;
                            mActivity.logindemo();
                        } else {
                            Toast.makeText(mActivity, resLogin.msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }
}
